package com.gitee.controller;

import com.gitee.annotation.AvoidRepeatableCommit;
import com.gitee.bo.CommentBo;
import com.gitee.bo.OrderBo;
import com.gitee.enums.ResponseEnum;
import com.gitee.service.ICommentService;
import com.gitee.service.IOrderService;
import com.gitee.utils.JwtUtil;
import com.gitee.vo.OrderVo;
import com.gitee.vo.PageVo;
import com.gitee.vo.ResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private IOrderService orderService;

    @Autowired
    private ICommentService commentService;

    @GetMapping
    public ResponseVo<PageVo<OrderVo>> listOrder(HttpServletRequest request, Integer cookbookType, Integer orderStatus, Integer orderType,
                                            Integer pageNum, Integer pageSize) {
        Integer userId = JwtUtil.getUserId(request.getHeader("token"));
        PageVo<OrderVo> orderVoPageVo = orderService.pageOrderVos(cookbookType, orderStatus, orderType, userId, pageNum, pageSize);
        return ResponseVo.ok(orderVoPageVo);
    }

    @AvoidRepeatableCommit
    @PostMapping
    public ResponseVo<String> commitOrder(HttpServletRequest request, @RequestBody OrderBo orderBo) {
        Integer userId = JwtUtil.getUserId(request.getHeader("token"));
        orderService.createOrder(userId, orderBo);
        return ResponseVo.ok(ResponseEnum.COMMIT_SUCCESS);
    }

    @PutMapping("/cancel/{orderId}")
    public ResponseVo<String> cancelOrder(HttpServletRequest request, @PathVariable Integer orderId) {
        Integer userId = JwtUtil.getUserId(request.getHeader("token"));
        orderService.cancelOrder(orderId, userId);
        return ResponseVo.ok(ResponseEnum.CANCEL_SUCCESS);
    }

    @DeleteMapping("/{orderId}")
    public ResponseVo<String> deleteOrder(HttpServletRequest request, @PathVariable Integer orderId) {
        Integer userId = JwtUtil.getUserId(request.getHeader("token"));
        orderService.deleteOrder(orderId, userId);
        return ResponseVo.ok(ResponseEnum.DELETE_SUCCESS);
    }

    @PostMapping("/comment")
    public ResponseVo<String> addComment(HttpServletRequest request, @RequestBody CommentBo commentBo) {
        Integer userId = JwtUtil.getUserId(request.getHeader("token"));
        commentService.addComment(userId, commentBo);
        return ResponseVo.ok(ResponseEnum.ADD_SUCCESS);
    }

    @PutMapping("/comment")
    public ResponseVo<String> updateComment(HttpServletRequest request, @RequestBody CommentBo commentBo) {
        Integer userId = JwtUtil.getUserId(request.getHeader("token"));
        commentService.updateComment(userId, commentBo);
        return ResponseVo.ok(ResponseEnum.UPDATE_SUCCESS);
    }

    @DeleteMapping("/comment/{orderId}")
    public ResponseVo<String> deleteComment(HttpServletRequest request, @PathVariable Integer orderId) {
        Integer userId = JwtUtil.getUserId(request.getHeader("token"));
        commentService.deleteComment(userId, orderId);
        return ResponseVo.ok(ResponseEnum.DELETE_SUCCESS);
    }

}
